package com.wf.it.hpuser.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.wf.it.common.api.UserService;
import com.wf.it.common.entity.User;
import com.wf.it.common.vo.Result;
import com.wf.it.hpuser.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao dao;

    @Override
    public Result userLogin(String loginName,String loginPwd){
        User user=dao.findByLoginName(loginName);
        if(user==null)
            return new Result<>(401,null,"login fail");
        //if(!user.getLoginPwd().equals(loginPwd))
        if(!BCrypt.checkpw(loginPwd,user.getLoginPwd()))//对比
            return new Result<>(401,null,"login fail");
        return new Result(user);
    }


    @Override
    public Result findByLoginName(String loginName) {
        User user= dao.findByLoginName(loginName);
        return new Result(user);
    }

    @Override
    @LcnTransaction
    @Transactional
    public Result reg(User user) {
        user.setLoginPwd(BCrypt.hashpw(user.getLoginPwd(),BCrypt.gensalt()));
        user.setRole("Normal");
        user.setState(1);
        user.setBalance(0f);
        dao.save(user);
        return new Result(user);
    }

    @Override
    @LcnTransaction
    @Transactional
    public Result changeBalance(String userIdCard, int money) {
        dao.updateBalance(userIdCard,money);
        return new Result();
    }
}
